/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.controllers;

import cz.muni.fi.pa036is.dto.ComplaintDetailTO;
import cz.muni.fi.pa036is.dto.ComplaintFormTO;
import cz.muni.fi.pa036is.dto.ComplaintGridTO;
import cz.muni.fi.pa036is.dto.LoginDTO;
import cz.muni.fi.pa036is.dto.MatchDetailTO;
import cz.muni.fi.pa036is.dto.MatchFormTO;
import cz.muni.fi.pa036is.dto.RoundGridTO;
import cz.muni.fi.pa036is.dtoconverters.MatchConverter;
import cz.muni.fi.pa036is.servicesinterface.ComplaintService;
import cz.muni.fi.pa036is.servicesinterface.MatchService;
import cz.muni.fi.pa036is.servicesinterface.PersonService;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 *
 * @author jostri
 */
@Controller
@SessionAttributes("complaint")
public class ComplaintController {

	@Autowired
	private ComplaintService complaintService;
	@Autowired
	private MatchService matchService;
	@Autowired
	private PersonService personService;

	@RequestMapping(value = "/admin/complaint/complaint")
	public String detail(ModelMap model) {

		List<ComplaintGridTO> complaints = this.complaintService.getAllComplaints();
		for (ComplaintGridTO c : complaints) {
			MatchDetailTO match = this.matchService.getMatchDetail(c.getMatchId());
			match.setPlayer1(this.personService.getPersonDetail(match.getPlayer1Id()));
			match.setPlayer2(this.personService.getPersonDetail(match.getPlayer2Id()));

			c.setMatch(match);
		}

		model.addAttribute("complaints", complaints);
		model.addAttribute("login", new LoginDTO());

		return "/admin/complaint/complaint";
	}

	@RequestMapping(value = "/admin/complaint/edit", params = "matchId")
	public String edit(@RequestParam("matchId") Long id, ModelMap model) {

		ComplaintFormTO complaint = new ComplaintFormTO();
		complaint.setTimestamp(new Timestamp(new Date().getTime()));
		complaint.setMatchId(id);

		model.addAttribute("complaint", complaint);

		MatchDetailTO match = this.matchService.getMatchDetail(id);
                model.addAttribute("login", new LoginDTO());
		model.addAttribute("match", match);
		model.addAttribute("player1", this.personService.getPersonDetail(match.getPlayer1Id()));
		model.addAttribute("player2", this.personService.getPersonDetail(match.getPlayer2Id()));

		return "/admin/complaint/edit";

	}

	@RequestMapping(value = "/admin/complaint/edit", method = RequestMethod.POST)
	public String edit(@Validated @ModelAttribute("complaint") ComplaintFormTO complaint, BindingResult result, ModelMap model,
			HttpSession session) {

		if (result.hasErrors()) {
			model.put("complaint", complaint);
			MatchDetailTO match = this.matchService.getMatchDetail(complaint.getMatchId());
			model.addAttribute("player1", this.personService.getPersonDetail(match.getPlayer1Id()));
			model.addAttribute("player2", this.personService.getPersonDetail(match.getPlayer2Id()));


			return "/admin/complaint/edit?matchId=" + complaint.getMatchId();
		}

		this.complaintService.addComplaint(complaint);

		return "redirect:/admin/match/detail?id=" + complaint.getMatchId();
	}

	@RequestMapping(value = "/admin/complaint/detail", params = "id")
	public String detail(@RequestParam("id") Long id, ModelMap model) {

		ComplaintDetailTO complaint = this.complaintService.getComplaintDetailTO(id);

		model.addAttribute("complaint", complaint);

		MatchDetailTO match = this.matchService.getMatchDetail(complaint.getMatchId());
		model.addAttribute("match", match);
		model.addAttribute("player1", this.personService.getPersonDetail(match.getPlayer1Id()));
		model.addAttribute("player2", this.personService.getPersonDetail(match.getPlayer2Id()));
		model.addAttribute("login", new LoginDTO());

		return "/admin/complaint/detail";

	}

	@RequestMapping(value = "/admin/complaint/remove", params = "id")
	public String remove(@RequestParam("id") Long id, ModelMap model) {

		this.complaintService.deleteComplaint(id);

		return "redirect:/admin/complaint/complaint";

	}
}
